package com.hongxin.nw.netty.utils;

import com.alibaba.fastjson.JSONObject;
import com.hongxin.nw.netty.config.ApplicationContextProvider;
import com.hongxin.nw.netty.values.Catalogue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @remark MQ 发送消息
 * @author: GuoXiaolin
 * @date 2019/6/14 9:43
 * @Version 1.0
 * @Descibe
 */
@Slf4j
@Service
public class MqSender {

    @Value("${nw.mq.type}")
    private String evm;

    public JSONObject send(String pro, JSONObject json){
        try {
            //获取的ben对象转为你需要的对象
            AmqpTemplate rabbitTemplate = ApplicationContextProvider.getBean(AmqpTemplate.class);//使用工具手动注入注入对象信息
            rabbitTemplate.convertAndSend(Catalogue.TOPIC_EXCHANGE + evm, pro + evm, json);
            return JSONUtils.getRoot(0, "mq发送成功", json);
        }catch (AmqpException e){
            return JSONUtils.getRoot(-1, "mq发送失败:" + e.getMessage(), json);
        }
    }

}
